package com.aegis.manage.service.sys.menu;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.aegis.manage.utils.AuthUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.aegis.common.mybatis.base.BaseServiceImpl;
import com.aegis.common.mybatis.constants.DeleteFlagEnum;
import com.aegis.common.utils.AssertUtil;
import com.aegis.manage.api.request.AddMenuRequest;
import com.aegis.manage.api.request.QueryMenuRequest;
import com.aegis.manage.api.vo.MenuVO;
import com.aegis.manage.mapper.SysMenuMapper;
import com.aegis.manage.model.dos.SysMenu;
import com.aegis.manage.model.dto.QuerySysMenuDTO;
import com.aegis.manage.service.sys.role.SysRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author renogo
 *
 * @Version 1.0
 * @Descritube
 */
@Service
public class SysMenuServiceImpl extends BaseServiceImpl<SysMenuMapper, SysMenu> implements SysMenuService {

    @Autowired
    private SysRoleService sysRoleService;

    @Override
    public void addMenu(AddMenuRequest request) {
        AssertUtil.operateFailed(selectCount(new LambdaQueryWrapper<SysMenu>()
                .eq(SysMenu::getMenuName, request.getMenuName())
                .eq(SysMenu::getParentId, request.getParentId())) > 0, "菜单名称已存在");
        SysMenu sysMenu = new SysMenu();
        BeanUtil.copyProperties(request, sysMenu);
        AssertUtil.operateFailed(!save(sysMenu));
    }

    @Override
    public List<MenuVO> selectMenuTree(QueryMenuRequest request) {
        List<SysMenu> sysMenuList = null;
        if (AuthUtil.isAdmin()) {
            sysMenuList = selectList(SysMenu::getDeleteFlag, DeleteFlagEnum.EXIST.getValue());
        } else {
            QuerySysMenuDTO querySysMenuDTO = new QuerySysMenuDTO();
            querySysMenuDTO.setMenuName(request.getMenuName());
            querySysMenuDTO.setStatus(request.getStatus());
            sysMenuList = getBaseMapper().selectMenuList(querySysMenuDTO);
        }
        return buildMenuTree(sysMenuList);
    }


    protected List<MenuVO> buildMenuTree(List<SysMenu> sysMenuList) {
        Map<String, MenuVO> menuMap = new HashMap<>();
        List<MenuVO> result = new ArrayList<>();
        for (SysMenu sysMenu : sysMenuList) {
            MenuVO menuVO = new MenuVO();
            BeanUtil.copyProperties(sysMenu, menuVO);
            if (menuVO.getParentId() == null) {
                result.add(menuVO);
                result.sort((o1, o2) -> {
                    if (o1.getPriority().equals(o2.getPriority())) {
                        return 0;
                    }
                    //下面是升序
                    if (o1.getPriority() > o2.getPriority()) {
                        return 1;
                    }
                    return -1;
                });
            }
            menuMap.put(menuVO.getId(), menuVO);
        }

        for (MenuVO activityTask : result) {
            Integer parentId = activityTask.getParentId();
            if (parentId == null) {
                continue;
            }
            MenuVO parentTask = menuMap.get(parentId);
            if (parentTask == null) {
//                log.info("父节点被屏蔽，子节点将无效");
                continue;
            }
            List<MenuVO> children = parentTask.getChildren();
            if (CollectionUtil.isEmpty(children)) {
                children = new ArrayList<>();
            }
            children.add(activityTask);
            children.sort((o1, o2) -> {
                if (o1.getPriority().equals(o2.getPriority())) {
                    return 0;
                }
                //下面是升序
                if (o1.getPriority() > o2.getPriority()) {
                    return 1;
                }
                return -1;
            });
            parentTask.setChildren(children);
        }
        return result;
    }


}
